package cn.shengchao.wecom.dubbo.consumer;

import cn.dev33.satoken.same.SaSameUtil;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

/**
 * @program: examstar-manger-api
 * @description: Sa-Token 整合 Dubbo Consumer端过滤器
 * @author: Xiong weiqiang
 * @create: 2025-05-27 17:29
 */
@Activate(group = {CommonConstants.CONSUMER}, order = -10000)
public class SameTokenDubboConsumerFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        System.out.println("SameTokenDubboConsumerFilter: setToken To context" + SaSameUtil.getToken());
        // 追加 Same-Token 参数
        RpcContext.getServerContext().setAttachment(SaSameUtil.SAME_TOKEN, SaSameUtil.getToken());

        // 如果有其他自定义附加数据，如租户
        // RpcContext.getContext().setAttachment("tenantContext", tenantContext);

        // 开始调用
        return invoker.invoke(invocation);
    }

}